package com.lanou3g.k12.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.k12.entity.Supporter;
import com.lanou3g.k12.service.SupporterService;
import com.lanou3g.k12.utils.K12Result;
import com.lanou3g.k12.utils.K12ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
import java.util.UUID;

@RestController
public class SupporterController {
    @Autowired
    private SupporterService supporterService;

    //    查询
    @GetMapping("/findAll")
    public K12Result list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                          @RequestParam(value = "size", defaultValue = "30") Integer size) {
        try {
            Page<Object> objects = PageHelper.startPage(page, size);
            supporterService.findAll(page, size);
            return K12ResultUtils.ok(objects);
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }

    }

    //    新增客服
    @PostMapping("/insert")
    public K12Result insert(Supporter supporter) {
        try {
            String password = supporterService.insert(supporter);
            return K12ResultUtils.ok(password);
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }
    }

    //    根据id批量删除
    @DeleteMapping("/supporters/{ids}")
    public K12Result deleteSomeCourse(@PathVariable Long[] ids) {
        try {
            supporterService.deleteSomeSupporter(ids);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }
    }

    //    根据id删除
    @DeleteMapping("/supporter/{id}")
    public K12Result deleteById(@PathVariable Integer id) {
        try {
            supporterService.deleteById(id);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }

    //    禁用
    @PutMapping("/supporter/{id}")
    public K12Result updateById(@PathVariable Integer id, Supporter supporter) {
        try {
            supporterService.updateById(supporter);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }
    }
    //   重置密码
    @PutMapping("/resetPassword/{id}")
    public K12Result resetPassword(Supporter supporter,@PathVariable Long id) {
        try {
            supporterService.resetPassword(supporter);
            return K12ResultUtils.ok(supporter.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }
    }

}



